﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace EXAM
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Request.Url.DnsSafeHost.ToString() != "daf.tavicosoft.com" && Request.Url.DnsSafeHost.ToString() != "localhost" && Request.Url.DnsSafeHost.ToString() != "elearning.spe-abbott.com.vn" && Request.Url.DnsSafeHost.ToString() != "115.84.183.57")
                {
                    Response.Redirect("License.aspx");
                }

                if (Page.ToString().ToUpper() != "ASP.LOGIN_ASPX" && Page.ToString().ToUpper() != "ASP.LEARNING_ASPX" && Page.ToString().ToUpper() != "ASP.TEST_ASPX")
                {
                    LoadTestMenu();

                    if (Session["dsTestLog"] != null)
                        SaveResult("dsTestLog");
                }
                DTO.UserInfo usrinf = Session["UserGuest"] as DTO.UserInfo;
                if (usrinf != null)
                {
                    linkUser.InnerHtml = linkUser.InnerHtml.Replace("Change Password", "Welcome " + usrinf.Lastname + " " + usrinf.Firstname + "!");
                }
            }
            //CacelFocus();
            SetFocus();
            if (Page.ToString().ToUpper() != "ASP.LOGIN_ASPX" && Session["UserGuest"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
        }

        private void LoadTestMenu()
        {
            BUS.GadgetItemsControl gadCtr = new BUS.GadgetItemsControl();
            DataTable dt = gadCtr.GetParentName("Testing", ref sErr);

            lvDB.DataSource = dt;
            lvDB.DataBind();
        }

        private void SetFocus()
        {

            string url = Request.Url.PathAndQuery.ToLower().Replace("%20", " ");
            string aburl = Request.Url.AbsolutePath.ToLower();
            if (aburl.Contains("learn"))
            {
                url = "/default.aspx?typegadget=test review";
            }
            else if (aburl.Contains("test"))
            {
                url = "/default.aspx?typegadget=testing";
            }
            else if (aburl.Contains("news.aspx"))
            {
                url = "/news.aspx";
            }
            switch (url)
            {
                case "/login.aspx":
                    linkLogin.Style.Add("background-color", "rgb(255,255,255)");
                    linkLogin.Style.Add("color", "white");
                    linkLogin.Style.Add("font-weight", "bold");
                    break;
                case "/default.aspx?typegadget=testing":
                    linkTest.Style.Add("background-color", "rgb(255,255,255)");
                    linkTest.Style.Add("color", "white");
                    linkTest.Style.Add("font-weight", "bold");
                    break;
                case "/default.aspx?typegadget=test review":
                    linkTestPreview.Style.Add("background-color", "rgb(255,255,255)");
                    linkTestPreview.Style.Add("color", "white");
                    linkTestPreview.Style.Add("font-weight", "bold");
                    break;
                case "/user.aspx":
                    linkUser.Style.Add("background-color", "rgb(255,255,255)");
                    linkUser.Style.Add("color", "white");
                    linkUser.Style.Add("font-weight", "bold");
                    break;
                case "/news.aspx":
                    linkNews.Style.Add("background-color", "rgb(255,255,255)");
                    linkNews.Style.Add("color", "white");
                    linkNews.Style.Add("font-weight", "bold");
                    break;
                case "/qna.aspx":
                    linkQnA.Style.Add("background-color", "rgb(255,255,255)");
                    linkQnA.Style.Add("color", "white");
                    linkQnA.Style.Add("font-weight", "bold");
                    break;
            }
        }

        private void CacelFocus()
        {
            if (Request.UrlReferrer != null)
            {
                string url = Request.UrlReferrer.AbsolutePath.ToLower();
                switch (url)
                {
                    case "login.aspx":
                        linkLogin.Style.Remove("color");
                        break;
                }
            }
        }
        private void SaveResult(string _dsetName)
        {
            if (Session["FlagTest"] != null)
            {
                DataSet dset = Session[_dsetName] as DataSet;

                int index = (int)Session["QuestIndex"];
                BUS.Test_LogControl logCtr = new BUS.Test_LogControl();
                long idTest = Convert.ToInt64(dset.Tables["dtQuest"].Rows[0]["Test_ID"]);
                long idUrs = Convert.ToInt64(dset.Tables["dtQuest"].Rows[0]["User_ID"]);
                string type = Convert.ToString(dset.Tables["dtQuest"].Rows[0]["Type"]);
                if (type == "TMP" && idUrs != -1)
                    sErr = logCtr.Delete(idTest, idUrs, type);
                BUS.Test_Log_AnswerControl ansCtr = new BUS.Test_Log_AnswerControl();
                int No = logCtr.GetMaxNo() + 1;
                foreach (DataRow row in dset.Tables["dtQuest"].Rows)
                {
                    DTO.Test_LogInfo logInf = new DTO.Test_LogInfo(row);
                    DTO.UserInfo user = Session["UserGuest"] as DTO.UserInfo;
                    if (user == null)
                        Response.Redirect("Login.aspx?hander=save");
                    DataRow[] rows = dset.Tables["dtAnswer"].Select("Test_Log_No = " + logInf.No + " and Test_Log_Line = " + logInf.Line);
                    for (int i = rows.Length - 1; i >= 0; i--)
                    {
                        DataRow rowA = rows[i];
                        DTO.Test_Log_AnswerInfo aInf = new DTO.Test_Log_AnswerInfo(rowA);
                        aInf.Test_Log_No = No;
                        ansCtr.Add(aInf, ref sErr);
                        dset.Tables["dtAnswer"].Rows.Remove(rowA);
                    }
                    logInf.User_ID = user.ID;
                    logInf.No = No;
                    //logInf.Type = "";
                    logCtr.Add(logInf, ref sErr);

                }
            }
            if (sErr == "")
            {
                Session["QuestIndex"] = null;
                Session[_dsetName] = null;
                Session["Duration_Time"] = null;
                Session["FlagTest"] = null;
                Session["CountAns"] = null;
                //Response.Redirect("TestTemplate.aspx");
            }

        }
        private void LoadChild(Literal gad, string parent)
        {
            //BUS.GadgetItemsControl gadCtr = new BUS.GadgetItemsControl();
            //DataTable dt = gadCtr.GetParentName(ref sErr);
            //lvDB.DataSource = dt;
            //lvDB.DataBind();
            //foreach (DataRow row in dt.Rows)
            //{
            string typeGadget = "Testing";

            BUS.GadgetItemsControl itemCtr = new BUS.GadgetItemsControl();
            DataTable dtx = itemCtr.GetByGadget(parent, typeGadget, ref sErr);
            string child = "";
            foreach (DataRow row in dtx.Rows)
            {
                string tmp = "<li><a class='menuitem' href='{1}'>{0}</a></li>";
                string name = row["Name"].ToString();
                string link = row["Link"].ToString() == "" ? "#" : row["Link"].ToString();
                tmp = String.Format(tmp, name, link);
                child += tmp;
            }
            gad.Text = "<ul>" + child + "</ul>";
        }



        protected void lvDB_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item != null)
            {
                if (e.Item is System.Web.UI.WebControls.ListViewDataItem)
                {
                    DataRowView tmp = (DataRowView)((System.Web.UI.WebControls.ListViewDataItem)e.Item).DataItem;
                    Control x = e.Item.FindControl("contentChild");
                    if (x is Literal)
                        LoadChild((Literal)x, tmp["Parent"].ToString());
                }
            }
        }
    }
}
